package com.item.listener;

import javax.servlet.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;
import java.io.*;

public class ContextListener implements ServletContextListener {
	private Logger log = Logger.getLogger(ContextListener.class);
	private static ExecutorService executor = Executors.newCachedThreadPool();
	public static String rootPath = "";
	public static String rootPathForbaihe = "";
	public static String subPath = "/";
	public static String ip = "";
	//final ExpressHandler handlerExpress = new ExpressHandler();
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext servletContext = sce.getServletContext();
		rootPath = servletContext.getRealPath("/");
		rootPathForbaihe = new File(rootPath).getParentFile().getParent() + "/file/njufile/";
		//rootPath = new File(rootPath).getParentFile().getParent() + "/file/njufile/";
//		final BaiheHandler handler = new BaiheHandler();
//		final LectureHandler handlerLecture = new LectureHandler();
		
		log.info("��������" + servletContext.getContextPath());
		
		executor.execute(new Runnable() {
			public void run() {
//				boolean flagB = false;
//				boolean flagL = false;
//				File[] oldFiles = null;
//				try {
//					while(!Thread.interrupted()) {		
//						oldFiles = DeleteFile.directoryNum(rootPathForbaihe + "/baiheImg");
//						flagL = handlerLecture.doUpdatelist("nju");
//						//handler.doUpdatelist("nuaa");
//						//handler.doUpdatelist("hhu");
//						//handler.doUpdatelist("njust");
//						//handlerLecture.doUpdatelist("nju");
//						//handler.doUpdatelist("seu");
//						flagB = handler.doUpdatelist("nju");
//						cleanExpresses();
//						if(flagB && flagL) {
//							log.info("�ɹ����°ٺ�ʮ���б?����ͽ�����Ϣ");
//							DeleteFile.deleteDirectoryCount(oldFiles);
//						}
//						Thread.sleep(1000 * 60 * 5);
//					}
//				} catch(Exception e) {
//					log.error(e);
//				}
			}
		});
	}
	public void contextDestroyed(ServletContextEvent sce) {
		ServletContext servletContext = sce.getServletContext();
		log.info("�����ر�" + servletContext.getContextPath());
		executor.shutdown();
	}
	public static boolean verifyTime() {
		boolean flag = false;
		Date now = new Date(System.currentTimeMillis());
		DateFormat format = new SimpleDateFormat("HH:mm");
		if(format.format(now).equals("00:00"))
			flag = true;
		return flag;
	}
	public void cleanExpresses() {
//		Date now = new Date();
//		if("00:00".equals(BaseHandler.format_schedule.format(now))) {
//			if(handlerExpress.clearOldExpresses()) {
//				log.info("����ʱ��ݳɹ���");
//			} else {
//				log.info("����ʱ���ʧ�ܣ�");
//			}
//		}
	}
}
